# Contributor: Erik Larsson <who+alpine@cnackers.org>
# Contributor: Alexander Sack <asac@pantacor.com>
# Maintainer: Alexander Sack <asac@pantacor.com>
pkgname=tpm2-tss
pkgver=3.1.0
pkgrel=0
pkgdesc="TPM 2.0 TSS"
url="https://github.com/tpm2-software/tpm2-tss/"
pkgusers="tss"
pkggroups="tss"
arch="all"
license="BSD-2-Clause"
makedepends="automake autoconf libtool openssl1.1-compat-dev doxygen perl
	linux-headers cmocka-dev autoconf-archive
	json-c-dev curl-dev acl"
_depends_fapi="acl"
subpackages="
	$pkgname-doc
	$pkgname-mu
	$pkgname-sys
	$pkgname-esys
	$pkgname-tcti-device
	$pkgname-tcti-mssim
	$pkgname-tcti-swtpm
	$pkgname-tcti-cmd
	$pkgname-tcti-pcap
	$pkgname-rc
	$pkgname-tctildr
	$pkgname-fapi
	$pkgname-static
	$pkgname-dev
	"
install="tpm2-tss-fapi.pre-install tpm2-tss-fapi.pre-upgrade tpm2-tss-fapi.post-install tpm2-tss-fapi.pre-upgrade"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/tpm2-software/tpm2-tss/archive/$pkgver.tar.gz

	0001-FAPI-fix-bug-of-bad-type-casting.patch
	pcap_test_fix.patch
	0001-tests-enable-tcti-pcap-tests-on-big-endian-platforms.patch
	dl_test_fix.patch
	0001-configure-Check-system-with-busybox.patch
	musl-32bit-stat-workaround.patch
"

prepare() {
	default_prepare
	./bootstrap
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--without-udevrulesdir \
		--with-tctidefaultmodule=device \
		--with-tctidefaultconfig=/dev/tpmrm0 \
		--with-crypto=ossl \
		--enable-unit
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	rm -rf "$pkgdir"/etc/tmpfiles.d
	rm -rf "$pkgdir"/etc/sysusers.d
}

mu() {
	pkgdesc="TPM 2.0 Marshaling libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-mu.so.* "$subpkgdir"/usr/lib
}

sys() {
	pkgdesc="TPM 2.0 System API libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-sys.so.* "$subpkgdir"/usr/lib
}

esys() {
	pkgdesc="TPM 2.0 Enhanced System API libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-esys.so.* "$subpkgdir"/usr/lib
}

device() {
	pkgdesc="TPM 2.0 Device TCTI"
	provides="libtss2-tcti"
	provider_priority="100"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tcti-device.so.* "$subpkgdir"/usr/lib
}

mssim() {
	pkgdesc="TPM 2.0 Simulator TCTI"
	provides="libtss2-tcti"
	provider_priority="10"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tcti-mssim.so.* "$subpkgdir"/usr/lib
}

rc() {
	pkgdesc="TPM 2.0 RC libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-rc.so.* "$subpkgdir"/usr/lib
}

tctildr() {
	pkgdesc="TPM 2.0 TCTI loader libraries"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tctildr.so.* "$subpkgdir"/usr/lib
}

fapi() {
	depends="$_depends_fapi"
	pkgdesc="TPM 2.0 FAPI libraries"
	mkdir -p "$subpkgdir"/usr/lib "$subpkgdir"/etc/tpm2-tss "$subpkgdir"/var/lib
	mv "$pkgdir"/usr/lib/libtss2-fapi.so.* "$subpkgdir"/usr/lib
	mv "$pkgdir"/etc/tpm2-tss/fapi-config.json "$subpkgdir"/etc/tpm2-tss
	mv "$pkgdir"/etc/tpm2-tss/fapi-profiles "$subpkgdir"/etc/tpm2-tss
	mv "$pkgdir"/var/lib/tpm2-tss "$subpkgdir"/var/lib
}

swtpm() {
	pkgdesc="TPM 2.0 SWTPM TCTI"
	provides="libtss2-tcti"
	provider_priority="10"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tcti-swtpm.so.* "$subpkgdir"/usr/lib
}

cmd() {
	pkgdesc="TPM 2.0 CMD TCTI"
	provides="libtss2-tcti"
	provider_priority="10"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tcti-cmd.so.* "$subpkgdir"/usr/lib
}

pcap() {
	pkgdesc="TPM 2.0 PCAP TCTI"
	provides="libtss2-tcti"
	provider_priority="10"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libtss2-tcti-pcap.so.* "$subpkgdir"/usr/lib
}

sha512sums="
d8a5079040415c1eb7ceb0d1d3c6a55b28526d6f6a156f9eea0814ef67b2cfc64ac8c157591dd77d24bbf2b0531573d66e192b0f24903e7ee70f5ef695ff2bbe  tpm2-tss-3.1.0.tar.gz
c11e3f6092b6984ab30d22bde9e667323e4ca51fa87476a93f1d52938e0f8c238a8cffe7e02d13eb59f0c39ee65da972e852864845e474f9f2d2f659a742ca52  0001-FAPI-fix-bug-of-bad-type-casting.patch
1beec50db7e91361e4b06ee76bbb70fafaea0e532b1e3fbe27c6e0f36063f001f87c589eb93b43aeedce50e7d9d99068fdf32a8f82de81881408d3214ff60ba9  pcap_test_fix.patch
f3af1703a6797e7998eed7e6dd8c538095acc0cd8b7dcf4c923f38d4257cba45959ffd03e0a797d7a2e0e004e1812fdb36922cfd4e5c58007af15f0383e45080  0001-tests-enable-tcti-pcap-tests-on-big-endian-platforms.patch
a6b94c682d642f2337b53e3354e27d8492fa69c7f060cef833bd6c716c0dbafa4e59ffdaa64c109f09b7796d59e3c42cddadead320e9a6305ce03afeb1ef8471  dl_test_fix.patch
8beb205681abee0832dd9bd02ab0d637146112649a6031a65df4ded02494381d91666cca67379000d316ef61a072d9db1e894f4fc5eb38fcb8f26ded9ac7b4f6  0001-configure-Check-system-with-busybox.patch
abdfb1100e8f26bd745e083c12c6cef9d0e7c6d62adb2d97c24b55846849a1ca2a53f7e1fd5819668301513705e6d4c7342bc00ede4d258fbae617fbeaf65e3e  musl-32bit-stat-workaround.patch
"
